<?php

class Backend_IndexController extends Zend_Controller_Action
{

    protected $_redirector = null;

    public function init()
    {
        /* Initialize action controller here */
        $this->_helper->layout->setLayout('backend/layout');
        $this->view->title = "";

        $this->_redirector = $this->_helper->getHelper('Redirector');

        // check session
        @session_start();
        if(!isset($_SESSION['admin']) && $this->getRequest()->getActionName() != "login"){
            $this->_redirector->gotoUrl('/backend/auth/login');
        }
        else{
            if(isset($_SESSION['admin'])){
                $this->view->session = $_SESSION['admin'];
            }
        }
    }

    public function indexAction()
    {
        // action body
        $this->view->title = "Home";
    }

    public function loginAction()
    {
        $this->view->title = "Login";

        $form = new Backend_Form_AdminLogin();
        $this->view->form = $form;

        if ($this->getRequest()->isPost()) {
            $postData = $this->getRequest()->getPost();

            // get admin
            $admin_db = new Backend_Model_DbTable_Admin();
            $admin = $admin_db->getAdminByEmail($postData['email']);

            // check existence
            if(!$admin){
                echo "Not Found!";
            }

            $_SESSION['admin'] = $admin;
            $this->_redirector->gotoUrl('/backend/');
        }
    }

    public function logoutAction()
    {
        if(isset($_SESSION['admin'])){
            unset ($_SESSION['admin']);
        }

        $this->_redirector->gotoUrl('/backend/');
    }
}







